package com.mintandleaf.webkits.validator;

import com.mintandleaf.webkits.data.WebKitsDataModel;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Validation processing worker
 * @author teguhh
 **/
public class WebKitsValidator {

    public HashMap<String,BaseValidator> listValidator=new HashMap<String, BaseValidator>();
    public HashMap<String,String> listValidationMessage=new HashMap<String, String>();
    public WebKitsValidator(){

    }

    /**
     * Adding validator for specified property name
     * @param paramPropertyName
     * @param paramValidator
     */
    public final void addValidator(String paramPropertyName,BaseValidator paramValidator){
        this.listValidator.put(paramPropertyName, paramValidator);
    }

    /**
     * Validate the data
     * @param paramDataModel
     * @return true or false on validation process
     */
    public boolean validate(WebKitsDataModel paramDataModel){
       boolean ret_=true;
       if(this.listValidator.size()>0){
           Iterator<String> iter=paramDataModel.getPropertyName().iterator();
           while(iter.hasNext()){
               String temp=iter.next();
               if(this.listValidator.get(temp)!=null)
                   if(this.listValidator.get(temp).validate(paramDataModel.get(temp)))
                       ;//if true do nothing if one is failed all validation view as unsuccessfull validation;
                   else{
                       this.listValidationMessage.put(temp, this.listValidator.get(temp).getErrorMessage());
                       ret_=false;
                   }
           }
       }
       return ret_;
    }

    /**
     * Get available error message
     * @return List of raised error
     */
    public Map getErrorMessage(){
        return this.listValidationMessage;
    }
    
}
